#include <string.h>
#include <muduo/base/Logging.h>
#include "MyTime.h"


#if 0
struct tm {
          int tm_sec;       /* 秒 – 取值区间为[0,59] */
          int tm_min;	/* 分 - 取值区间为[0,59] */
          int tm_hour;	/* 时 - 取值区间为[0,23] */
          int tm_mday;	/* 一个月中的日期 - 取值区间为[1,31] */
          int tm_mon;	/* 月份（从一月开始，0代表一月） - 取值区间为[0,11] */
          int tm_year;	/* 年份，其值等于实际年份减去1900 */
          int tm_wday;	/* 星期 – 取值区间为[0,6]，其中0代表星期天，1代表星期一，以此类推 */
          int tm_yday;	/* 从每年的1月1日开始的天数 – 取值区间为[0,365]，其中0代表1月1日，1代表1月2日，以此类推 */
          int tm_isdst;	/* 夏令时标识符，实行夏令时的时候，tm_isdst为正。不实行夏令时的进候，tm_isdst为0；不了解情况时，tm_isdst()为负。*/
};
#endif

//我现在的系统时间比服务器快8个小时，所以我取的本地时间都要减去28800，
//对于服务器过来的时间，如何需要打印小票的话，都得加上28800
void printTime(time_t timeData)
{
    char strTime[32];
    time_t now = timeData;
    struct tm *tm_now;
    tm_now = localtime(&now);
    strftime(strTime, sizeof(strTime), "%Y-%m-%d %H:%M:%S", tm_now);
    LOG_INFO << "now datetime = " << strTime;
}

void getDateTime(time_t timeData, char* buf)
{
    struct tm *tm_now;
    tm_now = localtime(&timeData);
    strftime(buf, 20, "%Y-%m-%d %H:%M:%S", tm_now);
}

void getCurrentDate(char* buf)
{
    time_t now = getCurrentTime();
    struct tm *tm_now = localtime(&now);
    strftime(buf, 20, "%Y-%m-%d", tm_now);
}

void getLastMonthDate(char* buf)
{
    time_t now = getCurrentTime();
    struct tm *tm_now;
    tm_now = localtime(&now);
    int year = tm_now->tm_year + 1900;
    int month = tm_now->tm_mon + 1;
    int day = tm_now->tm_mday;

    if(month == 1)
    {
        year --;
        month = 12;
    }

    sprintf(buf, "%04d-%02d-%02d", year, month, day);
}

void getTimeMS(time_t timeData, char* buf)
{
    struct tm *tm_now;
    tm_now = localtime(&timeData);
    strftime(buf, 10, "%M:%S", tm_now);
}

#ifdef TIME_ZONE_0
time_t getCurrentDay()
{
	time_t now = getCurrentTime();

	struct tm *tm_now;
	tm_now = localtime(&now);
	if(tm_now->tm_hour >= 16)
	{
		tm_now->tm_hour = 16;
		tm_now->tm_min = 0;
		tm_now->tm_sec = 0;
		return mktime(tm_now);
	}
	else
	{
		time_t now = getCurrentTime();
		struct tm *tm_now;
		tm_now = localtime(&now);
		tm_now->tm_hour = 0;
		tm_now->tm_min = 0;
		tm_now->tm_sec = 0;
		now = mktime(tm_now);
		now = now - 3600*24 + 3600*16;
		return now;
	}
}
#else

time_t getCurrentDay()
{
    time_t now = getCurrentTime();

    struct tm *tm_now;
    tm_now = localtime(&now);
    tm_now->tm_hour = 0;
    tm_now->tm_min = 0;
    tm_now->tm_sec = 0;
    return mktime(tm_now);
}
#endif

time_t getCurrentTime()
{
    time_t now;
    time(&now);
    return now;
}

int getCurrentDaySecond()
{
    time_t now = getCurrentTime();

    struct tm *tm_now;
    tm_now = localtime(&now);
    int second = tm_now->tm_hour*3600 + tm_now->tm_min*60 + tm_now->tm_sec;
    return second;
}

time_t getSpecialDay(int year, int month, int day)
{
    struct tm t;
    bzero(&t, sizeof(tm));
    t.tm_year = year - 1900;
    t.tm_mon = month - 1;
    t.tm_mday = day;
    t.tm_hour=0;
    t.tm_min=0;
    t.tm_sec=0;
    return mktime(&t);
}

void getCurrentYMD(int &year, int &month, int &day)
{
    time_t now;
    time(&now);

    struct tm *tm_now;
    tm_now = localtime(&now);
    year = tm_now->tm_year + 1900;
    month = tm_now->tm_mon + 1;
    day = tm_now->tm_mday;
}
